const App = getApp();
// 工具类
import Util from '../../utils/util.js';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isLogin: false,
    teamId: 0,
    cate:1,   //1代表处活动，2本队本队
    showPopup:false,
    peopleNum:1,
    list:[],
    page:1,// 当前页码
    last_page:1,
    scrollHeight: null,
    no_more: false, // 没有更多数据
    isLoading: true, // 是否正在加载中
    nowDate:Util.dateFormat('YYYY-mm-dd HH:MM:SS', new Date()),
    activity_id:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let that = this;
    that.setData({
      isLogin: App.checkIsLogin(),
    });

    if (that.data.isLogin) {
      that.setData({
        teamId: App.checkTeam(),
      });
       //设置页面高度
       that.setListHeight();
       //获取购物车列表
       that.getDataList();
       //当前时间
      // var nowDate = Util.dateFormat('YYYY-mm-dd HH:MM:SS', new Date())
       
    }
  },

 //获取数据列表
  getDataList(isPage, page){
  var obj = {
    team_id:0,
    page: page || 1,
    isfront:1
  }
  if(this.data.cate=="2"){
    obj.team_id = this.data.teamId
  }
  console.log(obj)
  let _this = this;
  App._get('activity/lists', obj, function(result) {
    let resList = result.data.list,
        dataList = _this.data.list;
    if (isPage == true) {
      _this.setData({
        list: dataList.concat(resList.data),
        isLoading: false,
        last_page: resList.last_page,
      });
    } else {
      _this.setData({
        list: resList.data,
        isLoading: false,
        last_page: resList.last_page,
      });
    }
  });
  },
  //捐款支付
  pay(e){
    //console.log(e.currentTarget.dataset.value)
    var time = Util.dateFormat('YYYY-mm-dd HH:MM:SS', new Date())
    console.log(this.data.nowDate)
  },
  // 报名弹窗
  onShowPopup(e){
    let that = this;
    console.log(e.currentTarget.dataset.id)
    that.setData({
      peopleNum:1,
      activity_id:e.currentTarget.dataset.id,
      showPopup: !that.data.showPopup
    }); 
  },
  radioChange(e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    this.setData({
      peopleNum:e.detail.value
    })


  },
  // tab切换
  onChangeNews(e){
    let that = this;
    that.setData({
      cate: e.currentTarget.dataset.id,
      list: [],
      page: 1,
      last_page:1,
      no_more: false,
      isLoading: true,
    }); 
    that.getDataList();
  },
  // 详情页跳转
  newsDetail(e){
    let nid = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: './detail/index?nid=' + nid,
    })
  },
//提交
formSubmit(e) {
  let formD = e.detail.value;//获取表单数据
  let formStatus = false
  for (let key in formD) {
    if(formD[key] == ""){
      formStatus = true
      break;
    }
  }
  if(formStatus){
    App.showError("姓名和电话不能空！");
    return;
  }
  let obj ={
    name:formD.userName,
    phone:formD.userTel,
    people_num:this.data.peopleNum|1,
    activity_id:this.data.activity_id
  }
  console.log(obj);
  let that =this
  App._post_form('activity/signup', obj, result => {
    //成功提示
    App.showSuccess(result.msg, function() {
      that.setData({
        showPopup: false
      }); 
      that.getDataList()
    });

  });
},
/**
   * 下拉到底加载数据
   */
  bindDownLoad() {
    // 已经是最后一页
    console.log("翻页");
    console.log("page =",this.data.page)
    console.log("last_page =",this.data.last_page)
    if (this.data.page >= this.data.last_page) {
      this.setData({
        no_more: true
      });
      return false;
    }
    // 加载下一页列表
    this.getDataList(true, ++this.data.page);
  },

  /**
   * 设置文章列表高度
   */
  setListHeight() {
    let systemInfo = wx.getSystemInfoSync(),
      rpx = systemInfo.windowWidth / 750, // 计算rpx
      tapHeight = Math.floor(rpx * 98), // tap高度
      scrollHeight = systemInfo.windowHeight - tapHeight; // swiper高度
    console.log(
      systemInfo.windowHeight
    );
    this.setData({
      scrollHeight
    });
  },
  /**
   * 自定义输入数量
   */
  onInputNum(e) {
    let _this = this,
      iptValue = e.detail.value;
    if (!Util.isPositiveInteger(iptValue) && iptValue !== '') {
      iptValue = 1;
    }
    _this.setData({
      peopleNum: iptValue
    });
    console.log( _this.data.peopleNum)
  },
  /**
   * 增加数量
   */
  onIncNumber(e) {
    let _this = this;
    App.saveFormId(e.detail.formId);
    _this.setData({
      peopleNum: ++_this.data.peopleNum
    })
  },

  /**
   * 减少数量
   */
  onDecNumber(e) {
    let _this = this;
    App.saveFormId(e.detail.formId);
    if (_this.data.peopleNum > 1) {
      _this.setData({
        peopleNum: --_this.data.peopleNum
      });
    }
  },
  /**
  * 分享当前页面
  */
 onShareAppMessage() {
    return {
      title: "活动报名",
      path: "pages/activity/index"
    };
  },
  /**
   * 分享到朋友圈
   */
  onShareTimeline() {
    return {
      title: "活动报名",
      path: "pages/activity/index"
    };
  },  

})